﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Exhibition.Common.CacheManager;
using Exhibition.Dao;
using Exhibition.Dao.ViewModel;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;

namespace Exhibition.Service
{
  public  class MembershipService
    {
      public static string GenerateName(string uname)
      {
          return uname + "@";
      }


      /// <summary>
      /// 刷新缓存用户
      /// </summary>
      /// <param name="uname"></param>
      /// <returns></returns>
      public static AdminUserModel FlushUser(string uname)
      {
          ICacheManager userCache = CacheFactory.GetCacheManager();
          string UsCacheID = GenerateName(uname) + "@U";

          if (userCache.Contains(UsCacheID))
              userCache.Remove(UsCacheID);

          return GetUser(uname);
      }
      /// <summary>
      /// 获取用户
      /// </summary>
      /// <returns></returns>
      public static AdminUserModel GetUser()
      {
          return GetAdminUser();

      }
      private static AdminUserModel GetAdminUser()
      {
          string[] userName = GetSplitLogName();
          if (userName.Length > 1)
          {
              return GetUser(userName[0] == "" ? "" : userName[0]);
          }
          else
          {
              return null;
          }
      }


      private static string[] GetSplitLogName()
      {
          string[] userName = HttpContext.Current.User.Identity.Name.Split('@');
          return userName;
      }


      public static AdminUserModel GetUser(string username)
      {
          ICacheManager userCache = CacheFactory.GetCacheManager();
          string UsCacheID = MembershipService.GenerateName(username) + "@U";
          AdminUserModel user = userCache.GetData(UsCacheID) as AdminUserModel;
          if (user == null)
          { 
              using (ExhibitionEntities edm = new ExhibitionEntities())
              {

                  IQueryable<AdminUserModel> admModel = null;
                  if (username == "admin")
                  {
                        admModel = from s in edm.E_AdminUser

                                  where s.UserName == username
                                  select new AdminUserModel()
                                  {
                                      CreateTime = s.CreateTime,
                                      CreateUserId = s.CreateUserId,
                                      Id = s.Id,
                                      PassWord = s.PassWord,
                                      RealName = s.RealName,
                                      UserName = s.UserName,
                                      Email = s.Email,
                                      Status = s.IsStatus,

                                  };
                  }
                  else
                  {

                      admModel = from s in edm.E_AdminUser
                                  from r in edm.E_Role
                                  where s.RoleId == r.Id
                                  && s.UserName == username
                                  select new AdminUserModel()
                                  {
                                      CreateTime = s.CreateTime,
                                      CreateUserId = s.CreateUserId,
                                      Id = s.Id,
                                      PassWord = s.PassWord,
                                      RealName = s.RealName,
                                      UserName = s.UserName,
                                      Email = s.Email,
                                      Status = s.IsStatus,
                                      UserSysRight = r.Funcs
                                  };
                  }
                  user = admModel.FirstOrDefault();
                   
                 
                  //var query = edm.E_AdminUser.Where(s => s.UserName == username);
                 
              }
              userCache.Add(UsCacheID, user, CacheItemPriority.High, null, new SlidingTime(TimeSpan.FromMinutes(180)));
          }
          return user;
      }


      public static UserModel GetLoginUser() {

        var userName = HttpContext.Current.User.Identity.Name;
        var  LoginUser = CommonCacheManager.GetCacheData<UserModel>("CurrentUser" + userName);
        return LoginUser;
      }

     
    }
}
